package com.neusoft.a312cxy.android18cxy.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.neusoft.a312cxy.android18cxy.R;
import com.neusoft.a312cxy.android18cxy.fragment.ClubFragment;
import com.neusoft.a312cxy.android18cxy.fragment.MainFragment;
import com.neusoft.a312cxy.android18cxy.fragment.PeiXunFragment;
import com.neusoft.a312cxy.android18cxy.fragment.ShaiTuFragment;
import com.neusoft.a312cxy.android18cxy.fragment.mySelfFragment;

/**
 * Created by Young on 2017/9/28.
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
    private LinearLayout perXun;
    private LinearLayout juLeBu;
    private LinearLayout index;
    private LinearLayout shaiTu;
    private LinearLayout mySelf;

    private ImageButton mImgperXun;
    private ImageButton mImgjuLeBu;
    private ImageButton mImgindex;
    private ImageButton mImgshaiTu;
    private ImageButton mImgmySelf;

    private Fragment mTab01;
    private Fragment mTab02;
    private Fragment mTab03;
    private Fragment mTab04;
    private Fragment mTab05;
  // int id = getIntent().getIntExtra( id , 0);
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();//初始化控件
        initEvent();//监听事件
        setSelect(0);//默认选择是哪个界面进行显示

    }

    private void initEvent()
    {
        perXun.setOnClickListener(this);//培训
        juLeBu.setOnClickListener(this);//俱乐部
        index.setOnClickListener(this);//主页
        shaiTu.setOnClickListener(this);//晒图
        mySelf.setOnClickListener(this);//个人

    }

    private void initView()
    {
        perXun= (LinearLayout) findViewById(R.id.id_buttom_peixun);
        juLeBu = (LinearLayout) findViewById(R.id.id_buttom_shaitu);
        index = (LinearLayout) findViewById(R.id.id_buttom_julebu);
        shaiTu= (LinearLayout) findViewById(R.id.id_buttom_index);
        mySelf= (LinearLayout) findViewById(R.id.id_buttom_wode);

        mImgperXun = (ImageButton) findViewById(R.id.id_peixun);
        mImgjuLeBu = (ImageButton) findViewById(R.id.id_julebu);
        mImgindex = (ImageButton) findViewById(R.id.id_index);
        mImgshaiTu = (ImageButton) findViewById(R.id.id_shaitu);
        mImgmySelf= (ImageButton) findViewById(R.id.id_wode);
    }
  /*  @Override

    protected void onResume() {
        int id = getIntent().getIntExtra("id", 4);
        if (id == 4) {
            Fragment fragmen = new mySelfFragment();
            FragmentManager fmanger = getSupportFragmentManager();
            FragmentTransaction transaction = fmanger.beginTransaction();
            transaction.replace(R.id.wide, fragmen);
            transaction.commit();
         //   mViewPager.setCurrentItem(4);//
            //帮助跳转到指定子fragment
            Intent i=new Intent();
            i.setClass(MainActivity.this,mySelfFragment.class);
            i.putExtra("id",2);
        }
        super.onResume();
    }
*/


    //管理fragement和修改颜色
    private void setSelect(int i)
    {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (mTab01 == null)
                {
                    mTab01 = new PeiXunFragment();
                    transaction.add(R.id.id_content, mTab01);
                } else
                {
                    transaction.show(mTab01);//如果为空就显示fragment
                }
                mImgperXun.setImageResource(R.drawable.ic_search_normal);
                break;
            case 1:
                if (mTab02 == null)
                {
                    mTab02 = new ClubFragment();
                    transaction.add(R.id.id_content, mTab02);
                } else
                {
                    transaction.show(mTab02);
                }
                mImgjuLeBu.setImageResource(R.drawable.ic_foodinfo_call);
                break;
            case 2:
                if (mTab03 == null)
                {
                    mTab03 = new ShaiTuFragment();
                    transaction.add(R.id.id_content, mTab03);
                } else
                {
                    transaction.show(mTab03);
                }
                mImgindex.setImageResource(R.drawable.ic_index_normal);
                break;
            case 3:
                if (mTab04 == null)
                {
                    mTab04 = new MainFragment();
                    transaction.add(R.id.id_content, mTab04);
                } else
                {
                    transaction.show(mTab04);
                }
                mImgshaiTu.setImageResource(R.drawable.ic_collect_normal);
                break;

            case 4:
               // int id = getIntent().getIntExtra( id , 0);
              //  if (id==4) {            Fragment fragment = new Infofragment();
               //     FragmentManager fmanger =getFragmentManager();
                    if (mTab05==null){
                    mTab05=new mySelfFragment();
                    transaction.add(R.id.id_content,mTab05);
                }else {
                    transaction.show(mTab05);
                }
                mImgmySelf.setImageResource(R.drawable.ic_self_normal);
                break;
            default:
                break;
        }
/*        nt id = getIntent().getIntExtra( id , 0);         if
            (id==1)
    {            Fragment fragmen = new Infofragment();
        FragmentManager fmanger =getFragmentManager();
        FragmentTransaction ftran =fmanger.beginTransaction();
        ftran.replace(R.id.franme_meun,fragmen);
        mit();
        info_meun.setChecked(true);
        li.setVisibility(View.GONE);        }*/

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (mTab01 != null)
        {
            transaction.hide(mTab01);
        }
        if (mTab02 != null)
        {
            transaction.hide(mTab02);
        }
        if (mTab03 != null)
        {
            transaction.hide(mTab03);
        }
        if (mTab04 != null)
        {
            transaction.hide(mTab04);
        }
        if (mTab05 != null)
        {
            transaction.hide(mTab05);
        }
    }

    @Override
    public void onClick(View v)
    {
        resetImgs();
        switch (v.getId())
        {
            case R.id.id_buttom_peixun:
                setSelect(0);
                break;
            case R.id.id_buttom_julebu:
                setSelect(1);
                break;
            case R.id.id_buttom_shaitu:
                setSelect(2);
                break;
            case R.id.id_buttom_index:
                setSelect(3);
                break;
            case R.id.id_buttom_wode:
                setSelect(4);
            default:
                break;
        }
    }

    private void resetImgs()
    {
        mImgperXun.setImageResource(R.drawable.ic_search_normal);
        mImgjuLeBu.setImageResource(R.drawable.ic_foodinfo_call);
        mImgindex.setImageResource(R.drawable.ic_index_normal);
        mImgshaiTu.setImageResource(R.drawable.ic_collect_normal);
        mImgmySelf.setImageResource(R.drawable.ic_self_normal);
    }

}